{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 更多小数位的无理数\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "005a5435-5bc3-4aa6-a168-10dd5050a655",
   "metadata": {},
   "source": [
    "该代码利用Python中的`mpmath`库来计算和输出具有高精度的小数表示，包括$\\pi$、自然对数的底数$e$和数值$2$的平方根$\\sqrt{2}$，分别保留其小数部分的前$1000$位。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **精度设置**：代码使用`mpmath`库的`mp.dps`属性来设置计算的精度。`dps`代表小数位数（decimal places），代码将其设置为$1000+1$，以确保包含1000位小数。小数点前的一位整数占用了1位，因此实际设置为$1001$位，以便得到完整的1000位小数。\n",
    "\n",
    "2. **输出$\\pi$的1000位小数部分**：$\\pi$的值约为$3.14159$，它是一个无理数，因此其小数部分是无限不循环的。代码使用`mp.pi`来访问高精度计算后的$\\pi$值，输出其小数点后的1000位精度：\n",
    "\n",
    "   $$\n",
    "   \\pi \\approx 3.1415926535\\ldots\n",
    "   $$\n",
    "\n",
    "3. **输出$e$的1000位小数部分**：$e$是自然对数的底数，约等于$2.71828$，也是一个无理数。代码使用`mp.e`来输出$e$的值，其小数点后的1000位高精度表示如下：\n",
    "\n",
    "   $$\n",
    "   e \\approx 2.7182818284\\ldots\n",
    "   $$\n",
    "\n",
    "4. **输出$\\sqrt{2}$的1000位小数部分**：平方根$\\sqrt{2}$的值约为$1.41421$，它也是无理数。代码通过`mp.sqrt(2)`计算并输出该值，保留其小数点后的1000位表示：\n",
    "\n",
    "   $$\n",
    "   \\sqrt{2} \\approx 1.4142135623\\ldots\n",
    "   $$\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码通过`mpmath`库的高精度设置功能，对数学常数$\\pi$、$e$、$\\sqrt{2}$的1000位小数部分进行了精确计算，并逐一输出这些值。这种高精度计算对科学研究和数学分析特别有用，尤其是在对无理数和数学常数的深入研究中。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4efec6c-1040-4403-be20-1e2c6c4948eb",
   "metadata": {},
   "source": [
    "## 设置小数精度并打印数学常数的高精度值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "56e094ab-1247-40d6-aa55-04bd30301a5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mpmath import mp  # 导入mpmath库中的mp模块\n",
    "\n",
    "mp.dps = 1000 + 1  # 设置小数精度为1000位（包含小数点前的一位）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "640d3e22-5924-4253-8331-48b798b41dbe",
   "metadata": {},
   "source": [
    "## 输出1000位π后的小数部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "991b0c99-9b32-4df7-98b8-59cf8d94021c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "print 1000 digits of pi behind decimal point\n",
      "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989\n"
     ]
    }
   ],
   "source": [
    "print('print 1000 digits of pi behind decimal point')  # 输出提示信息\n",
    "print(mp.pi)  # 输出π的高精度值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da9b8dbf-5609-4e68-ab3f-4ca76da5bff6",
   "metadata": {},
   "source": [
    "## 输出1000位e后的小数部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1485524-417e-4dc9-92b8-88197b43336b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "print 1000 digits of e behind decimal point\n",
      "2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509961818815930416903515988885193458072738667385894228792284998920868058257492796104841984443634632449684875602336248270419786232090021609902353043699418491463140934317381436405462531520961836908887070167683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350354\n"
     ]
    }
   ],
   "source": [
    "print('print 1000 digits of e behind decimal point')  # 输出提示信息\n",
    "print(mp.e)  # 输出e的高精度值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1beecb07-4a98-4b2f-8b46-a3772be3fa49",
   "metadata": {},
   "source": [
    "## 输出1000位$\\sqrt{2}$后的小数部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bcdbd4a5-42c7-4a5e-a9d3-703546004f5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "print 1000 digits of sqrt(2) behind decimal point\n",
      "1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372352885092648612494977154218334204285686060146824720771435854874155657069677653720226485447015858801620758474922657226002085584466521458398893944370926591800311388246468157082630100594858704003186480342194897278290641045072636881313739855256117322040245091227700226941127573627280495738108967504018369868368450725799364729060762996941380475654823728997180326802474420629269124859052181004459842150591120249441341728531478105803603371077309182869314710171111683916581726889419758716582152128229518488472\n"
     ]
    }
   ],
   "source": [
    "print('print 1000 digits of sqrt(2) behind decimal point')  # 输出提示信息\n",
    "print(mp.sqrt(2))  # 输出2的平方根的高精度值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
